//
//  BQMineHeaderView.m
//  BeeQuick
//
//  Created by student on 2017/7/9.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQMineHeaderView.h"
#import <Masonry.h>
#define KButtonW  self.frame.size.width/3
#define KButtonH  self.frame.size.height
@implementation BQMineHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];

    }
    return self;
}

- (void)setupUI{
    NSArray *imageNameArr = @[@"v2_my_order_icon",@"v2_my_coupon_icon",@"v2_my_message_icon"];
    NSArray *titleArr = @[@"我的订单",@"我的优惠券",@"我的消息"];
    [self addChildrenButtonWithImageName:imageNameArr title:titleArr];
}

- (void)addChildrenButtonWithImageName:(NSArray *)imageNameArr title:(NSArray*)titleArr{
    
    for (int i = 0; i < 3; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i * KButtonW, 0, KButtonW, KButtonH)];
        [button setImage:[UIImage imageNamed:imageNameArr[i]] forState:UIControlStateNormal];
        [button setTitle:titleArr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag = i;
        CGSize imageSize = button.imageView.frame.size;
        CGSize titleSize = button.titleLabel.frame.size;
        button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, -imageSize.height-5, 0);
        button.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height-5, 0, 0, -titleSize.width);
        [self addSubview:button];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i != 0) {
            UIImageView *lineImageView = [[UIImageView alloc]init];
            lineImageView.backgroundColor = [UIColor lightGrayColor];
            [self addSubview:lineImageView];
            [lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(8);
                make.bottom.equalTo(self).offset(-8);
                make.width.offset(1);
                make.leading.equalTo(self).offset(i * KButtonW);
            }];
        }
        if (i == 1) {
            UIImageView *redCycleImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"redCycle"]];
            [button addSubview:redCycleImageView];
            [redCycleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(button);
                make.trailing.equalTo(button).offset(-KButtonW/8.5);
            }];
            UILabel *showNumberLabel = [[UILabel alloc]init];
            showNumberLabel.text = @"1";
            showNumberLabel.textAlignment = NSTextAlignmentCenter;
            showNumberLabel.font = [UIFont systemFontOfSize:13];
            showNumberLabel.textColor = [UIColor redColor];
            showNumberLabel.frame = redCycleImageView.frame;
            [redCycleImageView addSubview:showNumberLabel];
        }
     }
   }
- (void)buttonAction:(UIButton*)button{
    if (self.HeaderOfBlock != nil) {
        self.HeaderOfBlock(button.tag);
    }
}

@end
